package school.community.controller;

import school.community.cache.TagCache;
import school.community.dto.InfoDTO;
import school.community.dto.QuestionDTO;
import school.community.model.Info;
import school.community.model.Question;
import school.community.model.User;
import school.community.service.InfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.List;

/**
 * Created by codedrinker on 2019/5/2.
 */
@Controller
@Slf4j
public class PublishController {

  @Autowired
  private InfoService infoService;

  @GetMapping("/publish/{id}")
  public String edit(@PathVariable(name = "id") Long id,
                     Model model) {
    InfoDTO infoDTO = infoService.getById(id);
    model.addAttribute("title", infoDTO.getTitle());
    model.addAttribute("description", infoDTO.getDescription());
    model.addAttribute("tag", infoDTO.getTag());
    model.addAttribute("id", infoDTO.getId());
    model.addAttribute("tags", TagCache.get());
    return "publish";
  }


  @GetMapping("/publish")
  public String publish(Model model) {
    model.addAttribute("tags", TagCache.get());
    return "publish";
  }

  @PostMapping("/publish")
  public String doPublish(
          @RequestParam(value = "title", required = false) String title,
          @RequestParam(value = "description", required = false) String description,
          @RequestParam(value = "tag", required = false) String tag,
          @RequestParam(value = "id", required = false) Long id,
          HttpServletRequest request,
          Model model) {
    model.addAttribute("title", title);
    model.addAttribute("description", description);
    model.addAttribute("tag", tag);
    model.addAttribute("tags", TagCache.get());

    //如果是管理员发布的内容，默认打上公告的标签
    HttpSession session = request.getSession();
    User admin = (User) session.getAttribute("admin");
    if (admin != null) {
      tag = "公告";
    }
    if (StringUtils.isBlank(title)) {
      model.addAttribute("error", "标题不能为空");
      return "publish";
    }
    if (StringUtils.isBlank(description)) {
      model.addAttribute("error", "问题补充不能为空");
      return "publish";
    }
    if (StringUtils.isBlank(tag)) {
      model.addAttribute("error", "标签不能为空");
      return "publish";
    }

    //如果不是非法的标签，也就是说在标签库种不存的标签，则不会返回一个空。如果标签合法，就会返回一个空
    if (admin == null) {
      String invalid = TagCache.filterInvalid(tag);
      if (StringUtils.isNotBlank(invalid)) {
        model.addAttribute("error", "输入非法标签:" + invalid);
        return "publish";
      }
    }
    User user = (User) request.getSession().getAttribute("user");
    if (user == null) {
      model.addAttribute("error", "用户未登录");
      return "publish";
    }
    Info info = new Info();

    //针对活动的标签特殊处理
    List<String> tagList = Arrays.asList("志工研学","主题讲座","实习兼职","文体活动","参数学习","社会实践","电子竞技","自习答疑","培训辅导","创新创业","晚会典礼","升学考研","才艺竞赛","运动会","招新选拔","演讲辩论","名人分享","交流研讨","巡展路演","志愿公益","就业招聘","科技竞赛","音乐会舞台剧","创意设计","党团活动");
    for (String tmp : tagList) {
      if (tag.contains(tmp)) {
        tag += ",活动";
        title = "【活动】" + title;
        break;
      }
    }
    if (tag.contains("活动")) {
      //用户发布的若是活动，则默认state为0
      info.setState(0);
    } else {
      //用户发布的若是普通信息，则默认state为0
      info.setState(1);
    }

    if ("公告".equals(tag)) {
      title = "【公告】" + title;
    }

    info.setTitle(title);
    info.setDescription(description);
    info.setTag(tag);
    info.setCreator(user.getId());
    info.setId(id);
    infoService.createOrUpdate(info);
    return "redirect:/";
  }
}
